package lib.bone.common.constant;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
public class ResponseCodeConst {
    public static ResponseCodeConst REQUEST_METHOD_ERROR = new ResponseCodeConst(114, "请求方式错误");

    public static ResponseCodeConst JSON_FORMAT_ERROR = new ResponseCodeConst(115, "JSON格式错误");
    /**
     * 0 - 1000 HTTP 状态码 & 项目自定义部分状态码
     */
    public static final ResponseCodeConst SUCCESS = new ResponseCodeConst(200, "操作成功!", true);
    //可以用于任务创建接口
    public static final ResponseCodeConst CREATED = new ResponseCodeConst(201,"创建成功!",true);
    //可以用于异步处理接口
    public static final ResponseCodeConst ACCEPTED = new ResponseCodeConst(202,"已接受!",true);
    //处理废弃接口
    public static final ResponseCodeConst MOVED_PERMANENTLY = new ResponseCodeConst(301,"资源已移动!");
    //处理维护接口
    public static final ResponseCodeConst FOUND = new ResponseCodeConst(302,"资源临时移动!");
    //使用代理
    public static final ResponseCodeConst USER_PROXY = new ResponseCodeConst(305,"请使用代理访问!");
    //服务器端无法理解的请求 & 参数错误
    public static final ResponseCodeConst ERROR_PARAM = new ResponseCodeConst(400, "参数异常!");

    public static final ResponseCodeConst BAD_REQUEST = new ResponseCodeConst(400,"错误的请求!");
    //未授权访问错误
    public static final ResponseCodeConst UNAUTHORIZED = new ResponseCodeConst(401,"未授权访问!");
    //禁止请求
    public static final ResponseCodeConst FORBIDDEN = new ResponseCodeConst(403,"拒绝执行!");
    //资源找不到
    public static final ResponseCodeConst NOT_FOUND = new ResponseCodeConst(404,"资源不存在!");
    //请求超时
    public static final ResponseCodeConst TIME_OUT = new ResponseCodeConst(408,"请求超时!");
    //错误的请求范围
    public static final ResponseCodeConst RANGE_ERROR = new ResponseCodeConst(416,"错误的请求范围!");
    //服务器错误
    public static final ResponseCodeConst SYSTEM_ERROR = new ResponseCodeConst(500, "系统错误");
    //错误的代理或者网关
    public static final ResponseCodeConst BAD_GATEWAY = new ResponseCodeConst(501,"网关错误!");
    //未完成的请求 正开发 / 维护
    public static final ResponseCodeConst DEVELOPMENT = new ResponseCodeConst(502, "此功能正在开发中");

    public static final ResponseCodeConst SERVICE_UNAVAILABLE = new ResponseCodeConst(502,"服务不可访问!");





    protected int code;

    protected String msg;

    protected boolean success;

    public ResponseCodeConst() {
    }

    protected ResponseCodeConst(int code, String msg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    /**
     * 异常公共构成器
     * @param responseCodeConst
     */
    public ResponseCodeConst(ResponseCodeConst responseCodeConst){
        this.code = responseCodeConst.getCode();
        this.msg = responseCodeConst.getMsg();
        this.success = responseCodeConst.success;
    }

    protected ResponseCodeConst(int code, String msg, boolean success) {
        super();
        this.code = code;
        this.msg = msg;
        this.success = success;
    }

    protected ResponseCodeConst(int code) {
        super();
        this.code = code;
    }



    public static void init() {
        log.info("ResponseCodeConst init....");
    }
    @Slf4j
    private static class ResponseCodeContainer{
        //常量状态码映射Map
        private static final Map<Integer,Class<? extends ResponseCodeConst>> RESPONSE_CODE_MAP = new HashMap<>();
        //注册异常状态码
        private static void register(Class<? extends ResponseCodeConst> eclass, int start, int end){
//            log.info(eclass.getSimpleName()+":开始注册");
            //越界判断
            if(start > end){
                String logmsg = eclass.getSimpleName() + " 定义状态码上下界异常";
                log.error(logmsg);
                throw new IllegalArgumentException(logmsg);
            }
            if(null!=RESPONSE_CODE_MAP.get(start)){
                String logmsg = "The range start code " + start + " has registed by "+RESPONSE_CODE_MAP.get(start).getSimpleName();
                log.error(logmsg);
                throw new IllegalArgumentException(logmsg);
            }else if(null!=RESPONSE_CODE_MAP.get(end)){
                String logmsg = "The range end code " + start + " has registed by "+RESPONSE_CODE_MAP.get(start).getSimpleName();
                log.error(logmsg);
                throw new IllegalArgumentException(logmsg);
            }else {

                for (int i=start;i<=end;i++){
                    RESPONSE_CODE_MAP.put(i,eclass);
                }
            }
        }
    }

}
